<?php

namespace Lib\Util;

class Cache {

    public static function read($file) {
        $path = self::getPath($file);
        if (file_exists($path)) {
            return file_get_contents($path);
        }

        return false;
    }

    public static function write($file, $data) {
        $path = self::getPath($file);
        $dir  = dirname($path);
        if (! is_dir($dir)) {
            mkdir($dir, 0777, true);
        }
        return file_put_contents($path, $data);
    }

    public static function delete($file) {
        $path = self::getPath($file);
        $path = preg_replace('/(\.[a-z]+)$/', '_*$1', $path);
        $deleted = true;
        foreach (glob($path) as $file) {
            $deleted = $deleted && unlink($file);
        }
        return $deleted;
    }

    private static function getPath($file) {
         return DIR_ROOT . DS . 'tmp' . DS . $file;
    }

}

?>